import datetime


import datetime

def get_pass_week():
    today = datetime.date.today()
    return [(today - datetime.timedelta(days=i)).strftime('%Y-%m-%d') for i in range(7)]


def build_segments(records):
    """
    records: 一天内同一视频的所有记录，按 time 升序排列。
    每条记录是 dict，包含: time, action, isEyeOn, name
    """
    segments = []
    prev = None
    for curr in records:
        curr_time = curr['time'] if isinstance(curr['time'], datetime.datetime) else datetime.datetime.strptime(curr['time'], '%Y-%m-%d %H:%M:%S')

        if prev:
            segment = {
                'start_time': prev['time'],
                'end_time': curr_time,
                'status': get_status(prev),
                'name': prev['name']
            }
            segments.append(segment)

        prev = {'time': curr_time, 'action': curr['action'], 'isEyeOn': curr['isEyeOn'], 'name': curr['name']}

    # 补全最后一段，延续到当天 23:59:59
    if prev:
        end_of_day = prev['time'].replace(hour=23, minute=59, second=59)
        if prev['time'] < end_of_day:
            segments.append({
                'start_time': prev['time'],
                'end_time': end_of_day,
                'status': get_status(prev),
                'name': prev['name']
            })

    return segments

def get_status(record):
    if record['action'] == 0:
        return 'paused'
    elif record['isEyeOn']:
        return 'watching'
    else:
        return 'no_eye'
